package com.kfm.jdbc.day0330;

import java.io.*;
import java.util.Arrays;

public class Demo04 {

    public static void main(String[] args) {
//        serializale();
        unserializale();
    }

    public static void serializale() {
        // 序列化
        try (FileOutputStream fos = new FileOutputStream("d:\\io\\serializable.txt");
             ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            Student student = new Student("张三", 18, '男');
            oos.writeObject(student);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void unserializale() {
        // 反序列化
        try (FileInputStream fis = new FileInputStream("d:\\io\\serializable.txt");
             ObjectInputStream ois = new ObjectInputStream(fis);
        ){
            Student student = (Student) ois.readObject();
            System.out.println(student);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

class Student implements Serializable {
    private String name;

    // transient 修饰的属性不会被序列化
    private transient Integer age;

    private char gender;

    private double b;

    private int a;
    private static final long serialVersionUID = 1L;


    public Student(String name, int age, char gender){
        System.out.println("init()");
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender=" + gender +
                '}';
    }
}
